package com.gxa.employment.controller;

import com.alibaba.excel.EasyExcel;
import com.gxa.employment.common.Result;
import com.gxa.employment.common.StudentDataListener;
import com.gxa.employment.entity.dto.FilterConditionDto;
import com.gxa.employment.entity.dto.StudentImportDto;
import com.gxa.employment.entity.pojo.Delay;
import com.gxa.employment.entity.vo.PageVo;
import com.gxa.employment.entity.vo.RestoreVo;
import com.gxa.employment.entity.vo.StuClazzIvwVo;
import com.gxa.employment.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 学生信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-25
 */
@RestController
@RequestMapping("/student")
@Tag(name = "学生信息管理", description = "学生相关操作API")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    /**
     * 批量导入学生信息
     */
    @PostMapping("/upload")
    @Operation(summary = "批量导入学生信息")
    public Result uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            // 创建StudentDataListener对象，用于监听Excel读取过程
            StudentDataListener listener = new StudentDataListener(studentService);
            // 使用EasyExcel读取Excel文件
            EasyExcel.read(file.getInputStream(), StudentImportDto.class, listener).sheet().doRead();
            // 获取失败的记录
            List<StudentImportDto> failedRecords = listener.getFailedRecords();
            // 如果存在失败的记录
            if (!failedRecords.isEmpty()) {
                // 返回失败记录的数量
                return Result.error(500,"导入失败，共有" + failedRecords.size() + "条记录");
            }
            //导入成功后更新redis中的状态标记

            ValueOperations<String, Object> operations = redisTemplate.opsForValue();
            operations.set("StudentStatus:" , "修改过");


            // 返回导入成功的信息
            return Result.success("导入成功");
        } catch (IOException e) {
            // 如果发生IO异常，返回错误信息
            return Result.error(500,"导入失败，文件上传出错");
        }
    }


    /**
     * 模糊查询学生信息
     */
    @GetMapping("/info")
    @Operation(summary = "模糊查询学生信息")
    public Result studentInfo(){
        List<StuClazzIvwVo> studentInfo = studentService.getStudentInfo();
        return Result.success(studentInfo);
    }

    /**
     * 筛选学生信息
     */
    @PostMapping ("/filter")
    @Operation(summary = "筛选学生信息")
    public Result<PageVo<StuClazzIvwVo>> filterStudents(@RequestBody FilterConditionDto filterConditionDto) {
        System.out.println(filterConditionDto);
        PageVo<StuClazzIvwVo> students = studentService.getStudentsByConditions(filterConditionDto);
        return Result.success(students);
    }

    /**
     * 删除学生信息
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除学生信息")
    public Result delete(@PathVariable Integer id){
        studentService.deleteStudent(id);
        return Result.success("删除成功");
    }



    /**
     * 申请暂停就业
     */
    @PostMapping("/pause")
    @Operation(summary = "申请暂停就业信息")
    public Result pause(@RequestBody Delay delay) {
        Boolean pause = studentService.pause(delay);
        //修改成功后更新redis中的状态标记
        if(pause){
            ValueOperations<String, Object> operations = redisTemplate.opsForValue();
            operations.set("StudentStatus:" , "修改过");

        }
        return Result.success(pause);
    }

    /**
     * 申请恢复就业
     */
    @PostMapping("/restore")
    @Operation(summary = "申请恢复就业信息")
    public Result restore(@RequestBody RestoreVo restoreVo) {
        Boolean restore = studentService.restore(restoreVo);
        //修改成功后更新redis中的状态标记
        if(restore){
            ValueOperations<String, Object> operations = redisTemplate.opsForValue();
            operations.set("StudentStatus:" , "修改过");

        }
        return Result.success(restore);
    }

//    放弃就业
    @GetMapping("/abandon")
    @Operation(summary = "放弃就业信息")
    public Result abandon(@RequestParam Integer stuId) {
        Boolean abandon = studentService.abandon(stuId);
        //修改成功后更新redis中的状态标记
        if(abandon){
            ValueOperations<String, Object> operations = redisTemplate.opsForValue();
            operations.set("StudentStatus:" , "修改过");
        }
        return Result.success(abandon);
    }

}
